<template>
  <div class="mod-demo-echarts">
    <el-alert title="提示：后台通过redis存储数据，websocket推送给前端数据，echarts作为前端展示，实现数据实时统计效果图" type="warning" :closable="false">
    </el-alert>

    <el-row :gutter="20">
      <el-col :span="24">
        <el-card>
          <el-button type="info" @click="onOrOffHandlel" style="margin-left: 80%;">开始统计</el-button>
          <el-button type="primary" :loading="exportSuccess" @click="exportExcel" style="margin-left: 90%;">导出数据</el-button>
          <div id="J_chartLineBox" class="chart-box"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    data() {
      return {
        chartLine: null,
        chartPie: null,
        chartScatter: null,
        flag: 1,
        realTimeList: [],
        cfList: [],
        lolList: [],
        pubgList: [],
        exportSuccess: false
      }
    },
    mounted() {
      this.initChartLine()
    },
    created() {
      var that = this
      that.bus.$on('refreshTodoList', (res) => {
        console.log("------------------------------come in ----------------------------------")
        console.log(res)
        this.realTimeList = res.content.realTimeList
        this.cfList = res.content.cfList
        this.lolList = res.content.lolList
        this.pubgList = res.content.pubgList
        console.log(this.realTimeList)
        this.initChartLine()
        setTimeout(function () {}, 1500)
      })
    },
    activated() {
      // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
      if (this.chartLine) {
        this.chartLine.resize()
      }
    },
    methods: {
      // 获取调用统计几口
      onOrOffHandlel() {
        this.$http({
          url: this.$http.adornUrl('/statistics/realTimeData'),
          method: 'get',
        })
      },
      exportExcel() {
        this.exportSuccess = true;
        this.$http({
              url: this.$http.adornUrl('/export/aliExcelExport'),
              method: "get",
              responseType: 'arraybuffer'
            }).then(res => {
          let content = res.data; // 文件流
          let blob = new Blob([content], {
            type: 'application/octet-stream'
          });
          let fileName = '日志记录.xls';
          // 如果后端返回文件名
          // let contentDisposition = res.getResponseHeader['Content-Disposition'];
          // console.log(contentDisposition)
          // let fileName = decodeURI(contentDisposition.split('=')[1]);
          if ('download' in document.createElement('a')) { // 非IE下载
            let link = document.createElement('a');
            link.download = fileName;
            link.style.display = 'none';
            link.href = URL.createObjectURL(blob);
            document.body.appendChild(link);
            link.click();
            URL.revokeObjectURL(link.href); // 释放URL 对象
            document.body.removeChild(link);
            this.exportSuccess = false;
          } else { // IE10+下载
            navigator.msSaveBlob(blob, fileName);
            this.exportSuccess = false;
          }
        })

      },
      // 折线图
      initChartLine() {
        var option = {
          'title': {
            'text': '游戏实时在线人数'
          },
          'tooltip': {
            'trigger': 'axis'
          },
          'legend': {
            'data': ['穿越火线', '英雄联盟', '绝地求生']
          },
          'grid': {
            'left': '3%',
            'right': '4%',
            'bottom': '3%',
            'containLabel': true
          },
          'toolbox': {
            'feature': {
              'saveAsImage': {}
            }
          },
          'xAxis': {
            'type': 'category',
            'boundaryGap': false,
            'data': this.realTimeList
          },
          'yAxis': {
            'type': 'value'
          },
          'series': [{
              'name': '穿越火线',
              'type': 'line',
              'stack': '总量',
              'data': this.cfList
            },
            {
              'name': '英雄联盟',
              'type': 'line',
              'stack': '总量',
              'data': this.lolList
            },
            {
              'name': '绝地求生',
              'type': 'line',
              'stack': '总量',
              'data': this.pubgList
            },
          ]
        }
        this.chartLine = echarts.init(document.getElementById('J_chartLineBox'))
        this.chartLine.setOption(option, true)
        window.addEventListener('resize', () => {
          this.chartLine.resize()
        })
      },
    }
  }

</script>

<style lang="scss">
  .mod-demo-echarts {
    >.el-alert {
      margin-bottom: 10px;
    }

    >.el-row {
      margin-top: -10px;
      margin-bottom: -10px;

      .el-col {
        padding-top: 10px;
        padding-bottom: 10px;
      }
    }

    .chart-box {
      min-height: 400px;
    }
  }

</style>
